package com.tanhua.sso.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tanhua.oss.OssTemplate;
import com.tanhua.common.exception.BusinessException;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.vo.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Service
public class UserInfoService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private OssTemplate ossTemplate;

    /*
     * 填充用户信息 UserInfo
     * */
    public void registUserInfo(String token, Map<String, String> params) {
        //1.解析token,获取对应的用户信息,id moblie
        User user = userService.queryToken(token);

        //2.封装对象userinfo
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(user.getId());
        userInfo.setNickName(params.get("nickname"));
        userInfo.setBirthday(params.get("birthday"));
        userInfo.setCity(params.get("city"));
        //PS:用户信息补充(职业)
        userInfo.setOccupation(params.get("occupation"));
        //PS:用户信息补充(个性签名)
        userInfo.setPersonalSignature(params.get("personalSignature"));

        //3.执行持久化操作
        int i = userInfoMapper.insert(userInfo);
        if (i == 0) {
            throw new BusinessException(ErrorResult.registUserInfo());
        }

        /*//用户为新用户
        //记录用户当前登录的时间为用户 最近活跃时间
        //将tb_user_info表中的字段last_active_time(最近活跃时间)记录为:当前系统时间
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setLastActiveTime(new Date());
        UpdateWrapper wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("userId", user.getId());
        int i1 = userInfoMapper.update(userInfo, wrapper1);
        if (i1 == 0) {
            //数据库更新失败
            //提示用户: 最近活跃时间更新失败!
            throw new BusinessException(ErrorResult.updateLastActiveTimeFail());
        }*/
    }

    /*
       头像上传及保存
    *       1.保存
    *       2.头像上传
    *
        上传
            1.本地上传 2.三方上传
    * */
    public void head(String token, MultipartFile headPhoto) {
        //1.解析token,获取对应的用户信息,id moblie
        User user = userService.queryToken(token);

        //判断头像是否为人像
        /*UserInfo userInfo1 = new UserInfo();
        try {
            FaceEngineService faceEngineService = new FaceEngineService();
            if (faceEngineService.checkIsPortrait(headPhoto.getBytes())) {
                //识别成功
                //将tb_user_info表中的字段user_status(头像状态)记录为:1(通过)
                userInfo1.setLogoStatus(1);
                userInfoMapper.insert(userInfo1);
            } else {
                //识别失败
                //将tb_user_info表中的字段user_status(头像状态)记录为:2(拒绝)
                userInfo1.setLogoStatus(2);
                userInfoMapper.insert(userInfo1);
            }
        } catch (Exception e) {
            //当出现异常时,提示用户:人脸识别出现异常
            throw new BusinessException(ErrorResult.checkIsPortraitException());
        }*/

        //2.将头像上传
        String picUrl = ossTemplate.upload(headPhoto);
        //3.根据用户id更新头像logo
        UserInfo userInfo = new UserInfo();
        userInfo.setLogo(picUrl);
        //3.执行更新
        LambdaUpdateWrapper<UserInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserInfo::getUserId, user.getId());
        int i = userInfoMapper.update(userInfo, wrapper);
        if (i == 0) {
            throw new BusinessException(ErrorResult.head());
        }
    }

}
